package adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.jaeger.ninegridimageview.NineGridImageView;
import com.jaeger.ninegridimageview.NineGridImageViewAdapter;

import java.util.ArrayList;
import java.util.List;

import bean.CarFriendBean;
import utils.CircleNetworkImage;
import utils.MyImageCache;
import utils.MyUrl;
import utils.RequestQueueUtils;
import zhang.automobileservice.R;

/**
 * Created by Administrator on 2016/8/2.
 */
public class CarFriendAdapetr extends BaseAdapter {
    private Context context;
    private ArrayList<CarFriendBean> arrayList;
    private ArrayList <String> list;

    public CarFriendAdapetr(Context context, ArrayList<CarFriendBean> arrayList) {
        this.context = context;
        this.arrayList = arrayList;
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.carfriend_listitem, null);
            viewHolder.cni_image = (CircleNetworkImage) convertView.findViewById(R.id.cni_image);
            viewHolder.ngi_image = (NineGridImageView) convertView.findViewById(R.id.ngi_image);
            viewHolder.tv_imageright = (TextView) convertView.findViewById(R.id.tv_imageright);
            viewHolder.tv_support = (TextView) convertView.findViewById(R.id.tv_support);
            viewHolder.tv_createDate = (TextView) convertView.findViewById(R.id.tv_createDate);
            viewHolder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            viewHolder.iv_support = (ImageView) convertView.findViewById(R.id.iv_support);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tv_imageright.setText(arrayList.get(position).getUsername());
        viewHolder.tv_createDate.setText(arrayList.get(position).getCreateDate());
        viewHolder.tv_content.setText(arrayList.get(position).getContent());
        viewHolder.tv_support.setText(arrayList.get(position).getLikeCount());
        String carUrl = MyUrl.Font_Url+MyUrl.Image+arrayList.get(position).getHeadPic();
        RequestQueueUtils.exectueCircleNetWorkImageView(context,carUrl,viewHolder.cni_image);
        NineGridImageViewAdapter adapter = new NineGridImageViewAdapter() {
            @Override
            protected void onDisplayImage(Context context, ImageView imageView, Object o) {
                MyImageCache lruImageCache = new MyImageCache();
                ImageLoader imageLoader = new ImageLoader(RequestQueueUtils.getRequestQueue(context), lruImageCache);
                ((NetworkImageView) imageView).setImageUrl((String) o, imageLoader);
            }

            @Override
            protected void onItemImageClick(Context context, int index, List list) {
                super.onItemImageClick(context, index, list);
            }

            @Override
            protected ImageView generateImageView(Context context) {
                NetworkImageView networkImageView = new NetworkImageView(context);
                return networkImageView;
                //return super.generateImageView(context);
            }
        };
        viewHolder.ngi_image.setAdapter(adapter);
        String url = arrayList.get(position).getImgs();
        list = new ArrayList<>();
        String[] urlarray = url.split(",");
        for(int i=0;i<urlarray.length;i++){
            list.add(MyUrl.Font_Url+MyUrl.Image+urlarray[i]);
        }
        viewHolder.ngi_image.setImagesData(list);
        return convertView;
    }


    static class ViewHolder {
        public CircleNetworkImage cni_image;
        public TextView tv_imageright;
        public TextView tv_createDate;
        public TextView tv_content;
        public NineGridImageView ngi_image;
        public TextView tv_support;
        public ImageView iv_support;
    }


}
